package com.huiztech.watersoil.activity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.huiztech.watersoil.R;
import com.huiztech.watersoil.R.drawable;
import com.huiztech.watersoil.R.id;
import com.huiztech.watersoil.R.layout;
import com.huiztech.watersoil.activity.MainActivity.clickAutoXY;
import com.huiztech.watersoil.model.Stream;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
/**
 * 查询结果页面
 * 流域列表
 * @author alin
 *
 */
public class ListViewActivity extends ListActivity {
	
	private ImageButton returnButton;
	private TextView error;
	
	private ArrayList<Stream> streamList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		getListView();//在oncreate方法中调用getListView()代替掉上面的setContentView(R.layout.xxx)
		//setContentView(getListView());
		ListView listView = this.getListView();
		listView.setDividerHeight(0);
		listView.setCacheColorHint(Color.TRANSPARENT);//防止滚动时不变黑
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.title2);
		
        Resources res = getResources();
        Drawable drawable = res.getDrawable(R.drawable.bgcolor);
        this.getWindow().setBackgroundDrawable(drawable);
		
        error = (TextView)findViewById(R.id.error);
        returnButton = (ImageButton)findViewById(R.id.return_button);
        returnButton.setOnClickListener(new clickReturn());
        
        streamList = (ArrayList<Stream>)getIntent().getSerializableExtra("stream_list");

		if(streamList.size()==0){
			Toast.makeText(getApplicationContext(),"对不起，没有找到相应记录",Toast.LENGTH_SHORT).show();
		}
		
		SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.list_view,
				new String[]{"name","county","village","rainfall","area","code","latitude","longitude"},
				new int[]{R.id.name,R.id.county,R.id.village,R.id.rainfall,R.id.area,R.id.code,R.id.latitude,R.id.longitude});
		setListAdapter(adapter);
		
	}

	private List<Map<String, Object>> getData() {
		DecimalFormat format2 = new DecimalFormat("#0.00");// 设置小数点后2位
		DecimalFormat format4 = new DecimalFormat("#0.0000");// 设置小数点后4位
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();		
		for(Stream s:streamList){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", s.getName());
			map.put("county", "所属县： "+s.getCounty());
			map.put("village", "所属乡： "+s.getVillage_name());
			float rainfall = Float.parseFloat(s.getRainfall());
			map.put("rainfall", "多年平均年侵蚀性降雨量： "+format2.format(rainfall).toString()+"(mm)");
			float area = Float.parseFloat(s.getArea());
			map.put("area", "小流域面积： "+format2.format(area).toString()+" ");
			map.put("code", "小流域代码： "+s.getCode());
			map.put("latitude", "小流域纬度： "+format4.format(s.getLatitude()).toString());
			map.put("longitude", "小流域经度： "+format4.format(s.getLongitude()).toString());
			list.add(map);
		}
		return list;
	}
	
    @Override   
    protected void onListItemClick(ListView l, View v, int position, long id) {  
    	super.onListItemClick(l, v, position, id);
		Intent intent = new Intent();
		intent.putExtra("stream",streamList.get(position));
		intent.setClass(ListViewActivity.this,com.huiztech.watersoil.activity.DetailActivity.class);
		startActivity(intent);	
    }  
    
	class clickReturn implements OnClickListener{
		@Override
		public void onClick(View v) {
			finish();
		}
	}
}
